package com.uzhie.web.service.listener;

import com.uzhie.jt808.msg.entity.JT_0800;
import com.uzhie.web.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import java.io.IOException;

/**
 * Created by TangFei on 2018/10/11.
 */
public class MQPictureReceiveService implements MQReceiveService {

    @Autowired
    private PictureService pictureService;

    @Override
    public void onMessage(Message message) {
        LOGGER.fatal("收到图片上传信息");
        handlerPic(message);
    }

    private void handlerPic(Message msg) {
        try {
            BytesMessage bytesMessage = (BytesMessage) msg;
            LOGGER.info(" 收到了图片上传信息--->");
            byte[] body = new byte[(int) bytesMessage.getBodyLength()];
            LOGGER.info(" 图片上传消息长度 : [" + bytesMessage.getBodyLength() + "]--->");
            bytesMessage.readBytes(body);
            LOGGER.info("图片上传body消息内容为 : [" + body.toString() + " ]--->");
            String mSimNumber = msg.getStringProperty(PARAM);
            LOGGER.info(" 位置信息的@车辆内置SIM卡@ 卡号为 : [" + mSimNumber + "] --->");
            JT_0800 order = new JT_0800(body);
            pictureService.addPicture(mSimNumber, order);
        } catch (JMSException | IOException e) {
            LOGGER.error("图片保存异常", e);
        }
    }
}
